<template>
  <div style="width: 50%; margin: 20px auto; min-height: 600px">
    <div class="card">
      <el-form ref="formRef" :model="data.user" label-width="80px" style="padding: 20px 30px">
        <div v-if="data.flag">
          <div style="padding: 10px 10px; font-size: 20px; font-weight: bold">请先填写当前密保回答</div>
          <el-form-item prop="passwordProblem" label="密保问题" >
            <div>{{data.user.passwordProblem}}</div>
          </el-form-item>
          <el-form-item prop="passwordReply" label="密保回答">
            <el-input v-model="data.passwordReply" type="textarea" rows="3" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="isPasswordProblem">确定</el-button>
          </el-form-item>
        </div>
        <div v-else>
          <div style="padding: 10px 10px; font-size: 20px; font-weight: bold">请填写新的密保问题和回答</div>
          <el-form-item prop="passwordProblem" label="密保问题" >
            <el-input v-model="data.user.passwordProblem"/>
          </el-form-item>
          <el-form-item prop="passwordReply" label="密保回答">
            <el-input v-model="data.user.passwordReply" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="update">保存</el-button>
          </el-form-item>
        </div>

      </el-form>
    </div>

  </div>
</template>

<script setup>
import { reactive, defineEmits } from "vue";
import request from "@/utils/request";
import {ElMessage} from "element-plus";
const baseUrl = import.meta.env.VITE_BASE_URL

const data = reactive({
  user:  JSON.parse(localStorage.getItem('xm-user') || '{}'),
  flag: true,
  passwordReply: null
})

const emit = defineEmits(["updateUser"])
const update = () => {
  if (data.user.role === 'USER') {
    if (data.user.passwordProblem === null || data.user.passwordReply === null) {
      ElMessage.info("密保问题或者回答不能为空")
      return
    }
    request.put('/user/update', data.user).then(res => {
      if (res.code === '200') {
        ElMessage.success('更新成功')
        localStorage.setItem('xm-user', JSON.stringify(data.user))
        emit('updateUser')
      } else {
        ElMessage.success(res.msg)
      }
    })
  }
}

// 判断密保是否正确
const isPasswordProblem = () => {
  if (data.passwordReply === data.user.passwordReply) {
    ElMessage.success("密保回答正确")
    data.user.passwordProblem = null
    data.user.passwordReply = null
    data.flag = false
  } else {
    ElMessage.error("密保回答错误")
  }
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>